import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import WeaponTalent from "../popups/WeaponTalent";
import LocalStorage from "../../framework/utils/LocalStorage";
import Component from "../../framework/Component";
import LoadResData from "../../framework/LoadResData";
import { WxEvent } from "../../framework/wx/WxEvent";
import Context from "../../framework/Context";


@ccclass
export default class FbItem extends Component {

    @property(cc.Sprite)
    private bg: cc.Sprite = null;
    @property(cc.Prefab)
    private rolePrefab: cc.Prefab = null;
    @property(cc.Prefab)
    private monsterPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    private bloodPrefab: cc.Prefab = null;
    @property(cc.Node)
    private box: cc.Node = null;
    @property(cc.Node)
    private infoLabel: cc.Node = null;
    @property(cc.Node)
    private clipNode: cc.Node = null;
    private data = null;
    private timerList = [];
    private prefabList = [];
    private reward = 0;

    public setInfo(data) {
        this.data = data;
        this.showUI();
    }
    private showUI() {
        var fbData = LocalStorage.getObj("FBData");
        var curTime = cc.sys.now();
        cc.log("time", curTime);
        if (fbData[this.data]["id"] != 0) {
            cc.log("dddd", fbData[this.data]["id"])
            this.showCurStateUI(this.data, fbData[this.data]["id"])
        }
        else {
            this.box.active = false;
            this.clipNode.active = false;
            this.infoLabel.active = true;
            this.infoLabel.getChildByName("label").getComponent(cc.Label).string = "所有冷却已经完毕";
        }

    }
    private showCurStateUI(index, id) {
        var fbData = LocalStorage.getObj("FBData");
        var curTime = cc.sys.now();
        cc.log("time", curTime);
        var weaponData = LoadResData.weaponsData;
        var weaponLvData: any = LocalStorage.getObj("weaponLv");
        var num = 0, lv = 0;
        var idle_basetime = 0, idle_up = 0, idle_ratio = 0, idle_reward = 0;
        var timeRemaining = 0;
        num = Math.floor((curTime - fbData[index]["startTime"]) / 1000);
        for (let j = 0; j < weaponData.length; j++) {
            for (let i = 0; i < weaponLvData.length; i++) {
                if (fbData[index]["id"] == weaponData[j]["id"]) {
                    if (weaponData[j]["id"] == weaponLvData[i]["id"]) {
                        lv = weaponLvData[i]["lv"];
                        idle_basetime = weaponData[j]["idle_basetime"];
                        idle_up = weaponData[j]["idle_up"]; idle_reward
                        idle_ratio = weaponData[j]["idle_ratio"];
                        idle_reward = weaponData[j]["idle_reward"];
                    }
                }
            }
        }
        this.reward = idle_reward + idle_ratio * lv;
        timeRemaining = (idle_basetime + idle_up * lv) - num;
        if (timeRemaining > 0) {
            this.box.active = false;
            this.clipNode.active = true;
            this.infoLabel.active = false;
            if (this.prefabList.length > 0) {
                for (let a in this.prefabList) {
                    if (fbData[index]["id"] == this.prefabList[a]["id"]) {
                        continue;
                    }
                    else {
                        this.showAnmin(index, fbData[index]["id"]);
                        this.showBloodUI(timeRemaining, timeRemaining, fbData[index]["id"]);
                    }
                }
            }
            else {
                this.showAnmin(index, fbData[index]["id"]);
                this.showBloodUI(timeRemaining, timeRemaining, fbData[index]["id"]);
            }


        }
        else {
            this.showBoxUI(fbData[index]["id"]);
        }
    }
    private showBoxUI(id) {
        var fbData: any = LocalStorage.getObj("FBData");
        for (let i = 0; i < fbData.length; i++) {
            if (id == fbData[i]["id"]) {
                this
                this.box.active = true;
                this.clipNode.active = false;
                this.infoLabel.active = false;
                this.setClickListener(this.box, this.onClickBox);
            }
        }

    }
    private showAnmin(index, id) {
        var curRolePrefab = cc.instantiate(this.rolePrefab);
        var curMonsterPrefab = cc.instantiate(this.monsterPrefab);
        var curbloodPrefab = cc.instantiate(this.bloodPrefab);
        var curNode = this.clipNode;

        curRolePrefab.x = curNode.getChildByName("roleNode").x + 60;
        curRolePrefab.y = curNode.getChildByName("roleNode").y;

        curMonsterPrefab.x = curNode.getChildByName("monsterNode").x - 50;
        curMonsterPrefab.y = curNode.getChildByName("monsterNode").y;

        curbloodPrefab.x = curMonsterPrefab.x;
        curbloodPrefab.y = curMonsterPrefab.y + 70;
        cc.log("ddddd", curNode.getChildByName("roleNode"));

        curNode.getChildByName("roleNode").addChild(curRolePrefab);
        curNode.getChildByName("monsterNode").addChild(curMonsterPrefab);
        curNode.getChildByName("monsterNode").addChild(curbloodPrefab);

        this.prefabList.push({ "id": id, "curRolePrefab": curRolePrefab, "curMonsterPrefab": curMonsterPrefab, "curbloodPrefab": curbloodPrefab })

    }
    private showBloodUI(num, total, id) {
        var self = this;
        var curbloodPrefab = null;
        for (let i = 0; i < this.prefabList.length; i++) {
            if (id == this.prefabList[i]["id"]) {
                curbloodPrefab = this.prefabList[i]["curbloodPrefab"];
            }
        }
        curbloodPrefab.getComponent(cc.ProgressBar).progress = (num / total).toFixed(2);
        cc.log("showBloodUI", num, curbloodPrefab.getComponent(cc.ProgressBar).progress);
        cc.log("curbloodPrefab", curbloodPrefab);
        if (num > 0) {
            num--;
            var t = setTimeout(() => {
                self.showBloodUI(num, total, id);
            }, 1000);
            this.timerList.push(t);
        }
        else {
            this.showBoxUI(id);
        }
    }
    private onClickBox(e: cc.Event.EventTouch) {
        var clickName = e.currentTarget.parent;
        clickName.getChildByName("box").active = false;
        clickName.getChildByName("clipNode").active = false;
        clickName.getChildByName("infoLabel").active = true;
        clickName.getChildByName("infoLabel").getChildByName("label").getComponent(cc.Label).string = "所有冷却已经完毕";
        this.removeClickListener(clickName.getChildByName("box"));

        var playerData = LocalStorage.getObj("Player");
        var gold: number = playerData["gold"] + this.reward;
        var data = {
            "gold": gold,
            oneselfWeapon: playerData["oneselfWeapon"]
        }
        LocalStorage.setObj("Player", data);
        Context.get().messenger.sendMessage(WxEvent.ON_CHANGEGOLD, [gold]);
        var fbData: any = LocalStorage.getObj("FBData");
        fbData.splice(this.data, 1, { "id": 0, "startTime": 0 });
        LocalStorage.setObj("FBData", fbData);
    }
}
